home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / sman_1_1 / part01 / src / sless_file.c < prev    next >
C/C++ Source or Header  |  1991-07-29  |  5KB  |  314 lines

  1.  
  2. #include "sless_defs.h"
  3. #include "sless_help.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <libraries/dos.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. extern const char memory[];
  10.  
  11. Prototype FILE *        sopen(char *);
  12. Prototype void            sclose(FILE *);
  13. Prototype char *        sgets(char *, int, FILE *);
  14. Prototype char *        sgetline(FILE *);
  15. Prototype char            sgetc(FILE *);
  16. Prototype void            sseek(FILE *, int, int);
  17. Prototype char            sgetcur(FILE *);
  18. Prototype void            sseekbot(FILE *, int);
  19. Prototype void            sputs(char *);
  20.  
  21. #define FILEBUFF    2048
  22.  
  23. FILE *
  24. sopen(n)
  25. char *n;
  26. {
  27.     FILE *fp;
  28.  
  29.     show_line;
  30.     if (!(fp = malloc(sizeof(FILE))))
  31.     leave(12,memory);
  32.     if (n == NULL) {
  33.     if (helplen == -1)
  34.         helplen = strlen(helpstr);
  35.     fp->des = 0;
  36.     fp->bufflen = helplen;
  37.     fp->buff = helpstr;
  38.     } else {
  39.     if (!(fp->des = Open(n,MODE_OLDFILE))) {
  40.         free(fp);
  41.         return NULL;
  42.     }
  43.     if (!(fp->buff = malloc(FILEBUFF)))
  44.         leave(12,memory);
  45.     fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
  46.     }
  47.     fp->filepos = 0;
  48.     fp->pos = 0;
  49.     fp->unget = 0x100;
  50.     return fp;
  51. }
  52.  
  53. void
  54. sclose(fp)
  55. FILE *fp;
  56. {
  57.     show_line;
  58.     if (fp->des) {
  59.     Close(fp->des);
  60.     free(fp->buff);
  61.     }
  62.     free(fp);
  63. }
  64.  
  65. char
  66. sgetc(fp)
  67. FILE *fp;
  68. {
  69.     show_line;
  70.     if (fp->unget != 0x100) {
  71.     char c;
  72.     c = fp->unget;
  73.     fp->unget = 0x100;
  74.     fp->pos++;
  75.     return c;
  76.     }
  77.  
  78.     if (fp->pos >= fp->bufflen) {
  79.     fp->filepos += fp->bufflen;
  80.     fp->bufflen = fp->des ? Read(fp->des,fp->buff,FILEBUFF) : 0;
  81.     if (seof(fp))
  82.         return 0;
  83.     fp->pos = 0;
  84.     }
  85.     return fp->buff[fp->pos++];
  86. }
  87.  
  88. char *
  89. sgets(p,n,fp)
  90. char *p;
  91. int n;
  92. FILE *fp;
  93. {
  94.     char *q = p;
  95.  
  96.     show_line;
  97.     sgetcur(fp);
  98.     if (seof(fp))
  99.     return NULL;
  100.     while (q - p < n-1 && (*q++ = getc(fp)) != '\n') {
  101.     if (seof(fp)) {
  102.         *--q = 0;
  103.         return p;
  104.     }
  105.     }
  106.     *q = 0;
  107.     return p;
  108. };
  109.  
  110. #define START_SIZE  100
  111. #define UP_SIZE     20
  112.  
  113. char *
  114. sgetline(fp)
  115. FILE *fp;
  116. {
  117.     char *q = malloc(START_SIZE),*p;
  118.     int l = START_SIZE;
  119.  
  120.     show_line;
  121.     p = q;
  122.     sgetcur(fp);
  123.     if (seof(fp))
  124.     return NULL;
  125.     while ((*q++ = getc(fp)) != '\n' && *(q-1) != 12) {
  126.     if (seof(fp)) {
  127.         *(q-1) = 0;
  128.         return p;
  129.     }
  130.     if (q - p == l) {
  131.         if (!(p = realloc(p,l += UP_SIZE)))
  132.         leave(12,memory);
  133.         q = p + l - UP_SIZE;
  134.     }
  135.     }
  136.     *q = 0;
  137.     return p;
  138. }
  139.  
  140. char
  141. sgetcur(fp)
  142. FILE *fp;
  143. {
  144.     show_line;
  145.     if (fp->unget != 0x100) {
  146.     return fp->unget;
  147.     }
  148.  
  149.     if (fp->pos >= fp->bufflen) {
  150.     fp->filepos += fp->bufflen;
  151.     fp->bufflen = fp->des ? Read(fp->des,fp->buff,FILEBUFF) : 0;
  152.     if (seof(fp))
  153.         return 0;
  154.     fp->pos = 0;
  155.     }
  156.     return fp->buff[fp->pos];
  157. }
  158.  
  159. void
  160. sseek(fp,p,t)
  161. FILE *fp;
  162. int p,t;
  163. {
  164.     show_line;
  165.     fp->unget = 0x100;
  166.     switch (t) {
  167.     case (SEEK_SET) :
  168.         if (p >= fp->filepos && p < fp->filepos+fp->bufflen)
  169.         fp->pos = p - fp->filepos;
  170.         else {
  171.         if (!fp->des) {
  172.             if (p <= helplen) {
  173.             fp->filepos = 0;
  174.             fp->bufflen = helplen;
  175.             fp->pos = p;
  176.             } else
  177.             fp->bufflen = 0;
  178.         } else {
  179.             fp->filepos = p;
  180.             fp->pos = 0;
  181.             if (Seek(fp->des,p,OFFSET_BEGINNING) == -1)
  182.             fp->bufflen = 0;
  183.             else
  184.             fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
  185.         }
  186.         }
  187.         break;
  188.     case (SEEK_CUR) :
  189.         if ((p < 0 && fp->pos >= -p) ||
  190.                      (p >= 0 && p + fp->pos < fp->bufflen))
  191.         fp->pos += p;
  192.         else {
  193.         if (!fp->des) {
  194.             if (p + fp->pos <= helplen) {
  195.             fp->filepos = 0;
  196.             fp->bufflen = helplen;
  197.             fp->pos += p;
  198.             } else
  199.             fp->bufflen = 0;
  200.         } else {
  201.             if (p < 0)
  202.             sseekbot(fp,fp->filepos + fp->pos + p);
  203.             else {
  204.             fp->filepos += fp->pos + p;
  205.             if (Seek(fp->des,fp->filepos,OFFSET_BEGINNING) == -1)
  206.                 fp->bufflen = 0;
  207.             else
  208.                 fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
  209.             fp->pos = 0;
  210.             }
  211.         }
  212.         }
  213.         break;
  214.     case (SEEK_END) :
  215.         if (!fp->des) {
  216.         if (p >= 0 && -p <= helplen) {
  217.             fp->filepos = 0;
  218.             fp->bufflen = helplen;
  219.             fp->pos = helplen+p;
  220.         } else
  221.             fp->bufflen = 0;
  222.         } else {
  223.         Seek(fp->des,p,OFFSET_END);
  224.         fp->filepos = Seek(fp->des,0,OFFSET_CURRENT);
  225.         fp->pos = 0;
  226.         fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
  227.         }
  228.         break;
  229.     }
  230. }
  231.  
  232. void
  233. sseekbot(fp,p)
  234. FILE *fp;
  235. int p;
  236. {
  237.     show_line;
  238.     if (p >= fp->filepos && p <= fp->filepos+fp->bufflen)
  239.     fp->pos = p - fp->filepos;
  240.     else {
  241.     if (!fp->des)
  242.         sseek(fp,p,SEEK_SET);
  243.     else {
  244.         fp->filepos = p - FILEBUFF + 1;
  245.         if (fp->filepos < 0)
  246.         fp->filepos = 0;
  247.         if (Seek(fp->des,fp->filepos,OFFSET_BEGINNING) == -1)
  248.         fp->bufflen = 0;
  249.         else
  250.         fp->bufflen = Read(fp->des,fp->buff,FILEBUFF);
  251.         fp->pos = p - fp->filepos;
  252.     }
  253.     }
  254. }
  255.  
  256. void
  257. sputs(p)
  258. char *p;
  259. {
  260.     chkabort();
  261.     Write(Output(),p,strlen(p));
  262. }
  263.  
  264. #ifdef __DEBUG__
  265.  
  266. Prototype void putint(int);
  267.  
  268. void
  269. putint(i)
  270. int i;
  271. {
  272.     int m = 1000000,f = FALSE;
  273.     char buf[9],*p = buf;
  274.  
  275.     for ( ; m ; m /= 10) {
  276.     if (f || i >= m || m == 1) {
  277.         *p++ = (i / m + '0');
  278.         f = TRUE;
  279.         i %= m;
  280.     }
  281.     }
  282.     *p = 0;
  283.     sputs(buf);
  284. }
  285.  
  286. Prototype void putchar(char);
  287.  
  288. void
  289. putchar(c)
  290. char c;
  291. {
  292.     Write(Output(),&c,1);
  293. }
  294.  
  295. #ifdef __TRACE__
  296.  
  297. Prototype void reportline(char *, int);
  298.  
  299. void
  300. reportline(name,line)
  301. char *name;
  302. int line;
  303. {
  304.     sputs("Passing by file ");
  305.     sputs(name);
  306.     sputs(" line ");
  307.     putint(line);
  308.     putchar('\n');
  309. }
  310.  
  311. #endif (__TRACE__)
  312.  
  313. #endif (__DEBUG__)
  314.